#!/usr/bin/env php
<?php
/*
 * This file is part of StaticReview
 *
 * Copyright (c) 2014 Samuel Parkinson <@samparkinson_>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @see http://github.com/sjparkinson/static-review/blob/master/LICENSE.md
 */
$included = include __DIR__ . '/../../../../../vendor/autoload.php';
if (!$included) {
    echo 'You must set up the project dependencies, run the following commands:' . PHP_EOL
        . 'curl -sS https://getcomposer.org/installer | php' . PHP_EOL
        . 'php composer.phar install' . PHP_EOL;

    exit(1);
}

// Installation:
// vendor/bin/static-review.php hook:install dev/tools/Magento/Tools/StaticReview/pre-commit .git/hooks/pre-commit

// Reference the required classes and the reviews you want to use.
use League\CLImate\CLImate;
use StaticReview\Reporter\Reporter;
use StaticReview\Review\Composer\ComposerLintReview;
use StaticReview\Review\General\LineEndingsReview;
use StaticReview\Review\General\NoCommitTagReview;
use StaticReview\Review\PHP\PhpLeadingLineReview;
use StaticReview\Review\PHP\PhpLintReview;
use StaticReview\Review\PHP\PhpCodeSnifferReview;
use Magento\Tools\StaticReview\PhpCsFixerReview;
use StaticReview\StaticReview;
use StaticReview\VersionControl\GitVersionControl;

$reporter = new Reporter();
$climate  = new CLImate();
$git      = new GitVersionControl();

// Apply review which modifies staged files first
$review = new StaticReview($reporter);
$phpCsFixer = new PhpCsFixerReview();
$review->addReview($phpCsFixer);
$review->review($git->getStagedFiles());

// Apply read-only review then
$review = new StaticReview($reporter);
$review->addReview(new LineEndingsReview())
    ->addReview(new PhpLeadingLineReview())
    ->addReview(new NoCommitTagReview())
    ->addReview(new PhpLintReview())
    ->addReview(new ComposerLintReview());
$review->review($git->getStagedFiles());

// Check if any matching issues were found.
if ($reporter->hasIssues()) {
    $climate->out('')->out('');

    foreach ($reporter->getIssues() as $issue) {
        $climate->red($issue);
    }

    $climate->out('')->red('вњ Please fix the errors above.');

    exit(1);
} else {
    $climate->out('')->green('вњ” Looking good.')->white('Have you tested everything?');

    exit(0);
}
